home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Main.bin
/
ProcessManager.java
< prev
next >
Wrap
Text File
|
1998-11-03
|
4KB
|
162 lines
package com.symantec.itools.lang;
import java.io.InputStream;
import java.io.IOException;
import com.symantec.itools.io.InterleavedReader;
/**
* @author Symantec Internet Tools Division
* @version 1.0
* @since VCafe 3.0
*/
public class ProcessManager
{
/**
* @since VCafe 3.0
*/
protected Process process;
public ProcessManager()
{
}
/**
* @param p TODO
* @since VCafe 3.0
*/
public int monitorLaunchedProcess(Process p)
{
InterleavedReader interleavedReader;
InputStream stdout;
InputStream stderr;
int exitStatus;
process = p;
interleavedReader = new InterleavedReader();
stdout = process.getInputStream();
stderr = process.getErrorStream();
interleavedReader.addInputStream(stdout);
interleavedReader.addInputStream(stderr);
try
{
String s;
while((s = interleavedReader.readLine()) != null)
{
InputStream lastRead = interleavedReader.lastStreamRead();
if(lastRead == stdout)
{
processCommandOutput(s);
}
else if(lastRead == stderr)
{
processCommandError(s);
}
}
}
catch(IOException ex)
{
ex.printStackTrace();
processCommandError("IOException: " + ex);
}
interleavedReader.removeInputStream(stdout);
interleavedReader.removeInputStream(stderr);
try
{
//replace process.waitFor(); with the fancy stuff below!
Waiter waiter;
Thread waiterThread;
waiter = new Waiter(p);
waiterThread = new Thread(waiter,"WaitOnSpawnedProcess");
waiterThread.start();
waiterThread.join();
return (waiter.getResult());
}
catch(InterruptedException ex)
{
process.destroy();
ex.printStackTrace();
}
exitStatus = process.exitValue();
process.destroy();
process = null;
return (exitStatus);
}
/**
* @since VCafe 3.0
*/
public void destroyProcess()
{
process.destroy();
}
/**
* @param str TODO
* @since VCafe 3.0
*/
protected void processCommandOutput(String str)
{
System.out.println(str);
}
/**
* @param str TODO
* @since VCafe 3.0
*/
protected void processCommandError(String str)
{
System.out.println(str);
}
final class Waiter
implements Runnable
{
// Process.waitFor() doesn't not respond to interrupt().
// This Waiter thread solves the problem
private Process process;
private int result;
Waiter(Process p)
{
this(p, -1);
}
Waiter(Process p, int defaultValue)
{
process = p;
result = defaultValue;
}
public void run()
{
try
{
result = process.waitFor();
}
catch(Throwable t)
{
}
}
final int getResult()
{
return (result);
}
}
}